#include "StdAfx.h"

#include ".\clocksource.h"
namespace Clock
{
	ClockSource::ClockSource(int FPS)
		:interval(1.0/FPS)
	{
		m_Average = 1;
		m_Fps = 0;
		m_FpsCount = 0;
		::QueryPerformanceFrequency(&frequency);
		::QueryPerformanceCounter(&last);
		m_LastCount = last;
	}
	void ClockSource::tick()
	{
		::QueryPerformanceCounter(&now);
		if((now.QuadPart -last.QuadPart)>interval*frequency.QuadPart)
		{
			this->notify();
			last=now;
			m_FpsCount++;
		}
		if((now.QuadPart -m_LastCount.QuadPart)>frequency.QuadPart * m_Average)
		{
			m_Fps = (int)(m_FpsCount / m_Average);
			m_FpsCount = 0;
			m_LastCount = now;
		}
	}
	void ClockSource::SetAverage(double average)
	{
		if(average > 0)
		{
			m_Average = average;
		}
	}
	int ClockSource::GetFPS(void)
	{
		return m_Fps;
	}
	ClockSource::~ClockSource(void)
	{
	}
}